﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using YmtAuth.AppService;
using YmtSystem.CrossCutting;

namespace YmtUserAuth.DataImport2
{
    public class DataImportUserMobile
    {
        public static void Import()
        {
            try
            {
                var filePath = System.Configuration.ConfigurationManager.AppSettings["data"];
                var fullPath = System.IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "data" + "\\" + filePath);
                Console.WriteLine("执行导入数据");
                var watch = Stopwatch.StartNew();
                var fields = ReadFile.SyncRead(fullPath);
                var data = FieldToUserBills.AdapterUserMobile(fields).OrderBy(e => e.UserId);
                Console.WriteLine("数据转换完毕 共 {0} 行， 耗时 {1} 秒 ", data.Count(), watch.Elapsed.TotalSeconds);
                Console.WriteLine("程序启动");
                YmatouFramework.Start();
                Console.WriteLine("程序启动完成，开始导入");
                var index = 0;
                //var mongo = LocalServiceLocator.GetService<YmtAuth.Repository.Mongodb.MongoConext>();
                data.Each(e =>
                {
                    ImportUser.ImportUserMobile(e.UserId, e.Mobile, "Ymt_Auth_201406", "ULC_201406", true);

                }, false, errorHandler: err =>
                {
                    YmatouLoggingService.Error("导入错误 {0}", err);
                });
                Console.WriteLine("end 耗时 {0}", watch.Elapsed);
            }
            catch (Exception ex)
            {
                Console.WriteLine("发生错误 {0}", ex);
            }
            Console.Read();
        }
    }


    public class UserMobile
    {
        public int UserId { get; set; }
        public string Mobile { get; set; }
    }
}
